﻿using System;
using System.Collections;
using System.Configuration;
using System.Data; 
using System.Web;
using System.Web.Security;
using System.Web.UI; 
using System.Web.UI.WebControls; 
using Microsoft.JCI.Common.Entity;
using Microsoft.JCI.Dal;
using Microsoft.JCI.Common.Utility;
using Telerik.WebControls;
using Microsoft.JCI.Bll;
using System.Collections.Generic;

namespace Microsoft.JCI.Portal.UserControl
{
    public partial class CustomerCtrl : System.Web.UI.UserControl
    {
        private string _CustomerKey = "UC_CustomerID";
        public Guid CustomerID
        {
            get 
            {
                if (ViewState[_CustomerKey] == null)
                {
                    return Guid.Empty;
                }

                return new Guid(ViewState[_CustomerKey].ToString());
            }
            set {
                ViewState[_CustomerKey] = value;
            }
        }

        public MadCustomerEntity CustomerEntity
        {
            get
            { 
                MadCustomerEntity tMadCustomerEntity = new MadCustomerEntity();
             
                    //tMadCustomerEntity.CreatedBy = "";
                    //tMadCustomerEntity.CreatedOn = DateTime.Now;
              
                tMadCustomerEntity.CustomerId = CustomerID;
                tMadCustomerEntity.Name = this.txtName.Text.Trim();
                tMadCustomerEntity.City = this.cbbCity.Value;
                tMadCustomerEntity.Province = this.cbbProvince.Value;
                tMadCustomerEntity.Country = this.txtDistinct.Text;
                tMadCustomerEntity.Gender = Convert.ToBoolean(Convert.ToInt32(this.cblGender.SelectedItem.Value));
                tMadCustomerEntity.OfficePhone = this.txtOfficePhone.Text;
                tMadCustomerEntity.HomePhone = this.txtHomePhone.Text;
                tMadCustomerEntity.PostCode = this.txtZip.Text;
                tMadCustomerEntity.Email = this.txtEmail.Text;
                tMadCustomerEntity.Address = this.txtAddress.Text;
                tMadCustomerEntity.Mobile = this.txtCellPhone.Text;
                tMadCustomerEntity.CustomerAttribute = ddlSmall.SelectedValue;
                return tMadCustomerEntity;                 
            }            
        }

        private string _Enabled = "Enabled";
        public bool Enabled
        {
            get
            {
                if (ViewState[_Enabled] != null)
                {
                    return Convert.ToBoolean(ViewState[_Enabled]);
                }
                return true;
            }
            set {
                ViewState[_Enabled] = value;
            }
        }

        public string ProvinceCode
        {
            get { return cbbProvince.SelectedValue; }
        }

        public event EventHandler<RadComboBoxSelectedIndexChangedEventArgs> OnProvinceSelectedIndexChanged;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitPage();

                GetCustomerInfo();
            }
        }

        private void InitPage()
        {
            SystemBll bll = new SystemBll();
            List<SysDictionaryEntity> dt = bll.GetDictionaryListFromCache(Enumerator.DictionaryType.Province);
            SysDictionaryEntity ent = new SysDictionaryEntity();
            ent.ValueCN = "请选择";
            ent.Code = "";
            dt.Insert(0, ent);

            this.cbbProvince.DataSource = dt;
            this.cbbProvince.DataTextField = "ValueCn";
            this.cbbProvince.DataValueField = "Code";
            this.cbbProvince.DataBind();

            if (this.cbbProvince.SelectedValue != "")
            {
                SysDictionaryEntity provinceentity = bll.GetDictionaryFromCache(this.cbbProvince.SelectedValue);
                if (provinceentity != null)
                {
                    List<SysDictionaryEntity> list = bll.GetDictionaryFromCacheByParentID(provinceentity.DictionaryId);
                    this.cbbCity.DataSource = list;
                    this.cbbCity.DataTextField = "ValueCn";
                    this.cbbCity.DataValueField = "Code";
                    this.cbbCity.DataBind();
                }
            }
        }

        public void GetCustomerInfo()
        {
            if (CustomerID == Guid.Empty)
            {
                return;
            }
             SystemBll bll = new SystemBll();
            MadCustomerEntity tMadCustomerEntity = new MasterDataBll().Get<MadCustomerEntity>(CustomerID);
            if(tMadCustomerEntity != null)
            {
                txtName.Text = tMadCustomerEntity.Name;
                this.cbbProvince.SelectedValue = tMadCustomerEntity.Province;
                if (this.cbbProvince.SelectedValue != "")
                { 
                    SysDictionaryEntity provinceentity = bll.GetDictionaryFromCache(this.cbbProvince.SelectedValue);
                    if (provinceentity != null)
                    {
                        List<SysDictionaryEntity> dt = bll.GetDictionaryFromCacheByParentID(provinceentity.DictionaryId);
                        this.cbbCity.DataSource = dt;
                        this.cbbCity.DataTextField = "ValueCn";
                        this.cbbCity.DataValueField = "Code";
                        this.cbbCity.DataBind();
                    }
                }
                this.cbbCity.SelectedValue = tMadCustomerEntity.City;
                if (!string.IsNullOrEmpty(tMadCustomerEntity.Country))
                {                                 
                    this.txtDistinct.Text = tMadCustomerEntity.Country;                                              
                }
                ddlSmall.SelectedValue = tMadCustomerEntity.CustomerAttribute;
                this.cblGender.SelectedValue = Convert.ToInt32(tMadCustomerEntity.Gender).ToString();

                this.txtOfficePhone.Text = tMadCustomerEntity.OfficePhone;
                this.txtHomePhone.Text = tMadCustomerEntity.HomePhone;
                this.txtZip.Text = tMadCustomerEntity.PostCode;
                this.txtEmail.Text = tMadCustomerEntity.Email;
                this.txtAddress.Text = tMadCustomerEntity.Address;
                this.txtCellPhone.Text = tMadCustomerEntity.Mobile;

            }
 
                DisabledForm(); 
        }

        private void DisabledForm()
        {
            if (!Enabled)
            {
                txtName.Enabled = false;
                cbbProvince.Enabled = false;
                cbbCity.Enabled = false;
                txtDistinct.Enabled = false;
                ddlSmall.Enabled = false;
                cblGender.Enabled = false;
                txtZip.Enabled = false;
                txtHomePhone.Enabled = false;
                txtOfficePhone.Enabled = false;
                txtCellPhone.Enabled = false;
                txtEmail.Enabled = false;
                txtAddress.Enabled = false;
            }
            else
            {
                txtName.Enabled = true;
                cbbProvince.Enabled = true;
                cbbCity.Enabled = true;
                txtDistinct.Enabled = true;
                ddlSmall.Enabled = true;
                cblGender.Enabled = true;
                txtZip.Enabled = true;
                txtHomePhone.Enabled = true;
                txtOfficePhone.Enabled = true;
                txtCellPhone.Enabled = true;
                txtEmail.Enabled = true;
                txtAddress.Enabled = true;
            }
        }

        protected void cbbProvince_SelectedIndexChanged(object o, Telerik.WebControls.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            if (string.IsNullOrEmpty(this.cbbProvince.SelectedValue))
            {
                return;
            }

            if (OnProvinceSelectedIndexChanged != null)
                OnProvinceSelectedIndexChanged(o, e);

            SystemBll bll = new SystemBll();
            SysDictionaryEntity entity = bll.GetDictionaryFromCache(Enumerator.DictionaryType.Province, this.cbbProvince.SelectedValue);

            this.cbbCity.Items.Clear();

            List<SysDictionaryEntity> dt = bll.GetDictionaryFromCacheByParentID(entity.DictionaryId);
            this.cbbCity.DataSource = dt;
            this.cbbCity.DataTextField = "ValueCn";
            this.cbbCity.DataValueField = "Code";
            this.cbbCity.DataBind();
        }

        //protected void cbbCustomer_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
        //{
        //    if (e.Text.Trim() == string.Empty) return;
        //    RadComboBox Customer = (RadComboBox)sender;
        //    DataTable dt = new MadCustomerDal().GetCustomer(e.Text.Trim());
        //    cbbCustomer.DataSource = dt;
        //    cbbCustomer.DataTextField = "Name";
        //    cbbCustomer.DataValueField = "CustomerId";
        //    cbbCustomer.DataBind();
        //}

        //protected void cbbCustomer_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        //{
        //    RadComboBox Customer = (RadComboBox)o;
        //    this.CustomerEntity = new MadCustomerDal().Get<MadCustomerEntity>(Customer.SelectedValue);
        //    this.ckNew.Checked = false;
        //}
    }
}